package com.jisu.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jisu.common.ResultVo;
import com.jisu.dto.CommentDTO;
import com.jisu.pojo.Comment;
import com.jisu.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CommentController {

    @Autowired
    private CommentService commentService;


    @PostMapping("/addComment")
    public ResultVo<Comment> addComment(@RequestBody Comment comment){
        return commentService.addComment(comment);
    }

    @DeleteMapping("/deleteComment")
    public ResultVo deleteComment(@RequestParam("ids") List<Integer> ids){
        return commentService.deleteComment(ids);
    }

    @PutMapping("/updateComment")
    public ResultVo updateComment(@RequestBody Comment comment){
        return commentService.updateComment(comment);
    }

    @PostMapping("/getComment")
    public ResultVo<IPage<Comment>> getComment(@RequestBody(required = false) CommentDTO commentDTO){
        return commentService.getCommentInPage(commentDTO);
    }

    @GetMapping("/getModelComments/{modelId}")
    public ResultVo<Map<String, Object>> getModelComments(@PathVariable Integer modelId) {
        // 获取该车型的所有评论
        List<Comment> comments = commentService.getCommentsByModelId(modelId);

        // 计算所有评分的平均分
        double averageScore = 0.0;
        int totalScores = 0;
        double sumScores = 0.0;

        if (comments != null && !comments.isEmpty()) {
            for (Comment comment : comments) {
                if (comment.getPowerScore() != null) {
                    sumScores += comment.getPowerScore();
                    totalScores++;
                }
                if (comment.getAppearanceScore() != null) {
                    sumScores += comment.getAppearanceScore();
                    totalScores++;
                }
                if (comment.getComfortScore() != null) {
                    sumScores += comment.getComfortScore();
                    totalScores++;
                }
                if (comment.getSpaceScore() != null) {
                    sumScores += comment.getSpaceScore();
                    totalScores++;
                }
                if (comment.getHandlingScore() != null) {
                    sumScores += comment.getHandlingScore();
                    totalScores++;
                }
                if (comment.getInteriorScore() != null) {
                    sumScores += comment.getInteriorScore();
                    totalScores++;
                }
            }

            if (totalScores > 0) {
                averageScore = sumScores / totalScores;
            }
        }

        // 封装返回数据
        Map<String, Object> result = new HashMap<>();
        result.put("comments", comments);
        result.put("averageScore", Math.round(averageScore * 10) / 10.0); // 保留一位小数


        return ResultVo.success("查询成功", result);
    }

}
